package com.dong.controller;
import com.alibaba.fastjson.JSON;
import com.dong.pojo.Iidar;
import com.dong.service.QueryIidarServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;

/*
微信的访问Controller
 */

@Controller
public class WxController {
    @Autowired
    QueryIidarServiceImpl queryIidarService;

    //激光雷达设备界面
    @RequestMapping("/wxSystem")
    @ResponseBody
    public String wxSystem(){
        System.out.println("微信激光雷达设备状态");
        Iidar queryIidar = queryIidarService.queryIidar();
        //Java对象转JSON字符串
        String str1 = JSON.toJSONString(queryIidar);
        return str1;
    }

    //激光雷达设备图片
    @RequestMapping("/wxPictures")
    @ResponseBody
    public HashMap<String,String> wxWeather(){
        System.out.println("微信激光雷达设备图片");
        Iidar queryIidar = queryIidarService.queryIidar();
        HashMap<String,String> bytes_pictures = new HashMap<>();
        //二进制图片
        byte[] weather_pictures = queryIidar.getWeather_pictures();
        byte[] Echosignal_pictures = queryIidar.getEchosignal_pictures();
        //base64编码
        String echosignal_base64 = Base64.getEncoder().encodeToString(Echosignal_pictures);
        String weather_base64 = Base64.getEncoder().encodeToString(weather_pictures);
        bytes_pictures.put("weatherPictures", weather_base64);
        bytes_pictures.put("echosignalPictures", echosignal_base64);
        return bytes_pictures;
    }

    //微信登录
    @RequestMapping("/login")
    @ResponseBody
    public String login(String username, String password){
        System.out.println ( "用户名:" + username + "密码:" + password );
        Subject subject = SecurityUtils.getSubject();
        //封装用户数据
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        //登录令牌
        try {
            System.out.println("微信登录成功");
//            subject.login(token);//执行登录方法，如果没有异常就ok了
            return "微信登录成功";
        } catch (UnknownAccountException e) {
            return "用户名不存在";
        } catch (IncorrectCredentialsException e) {
            return "密码不存在";
        }
    }
}
